var map = new AMap.Map("container", {
	resizeEnable: true
});

! function(e, $, mui) {
	"use strict";
	var config = {};
	// 定义页面
	var p = function(config) {
		var t = this;
		t.config = config;
		t.userInfo = null;
		t.page = {
			page: 0,
			perpage: 0,
			type: 0,
			start: "",
			end: "",
			city: city,
			isStart: true
		};
		t.element = {
			container: $('#container'),
			start: $('#start'),
			startItem: $('#startItem'),
			end: $("#end"),
			panel: $('#panel'),
			buttons: $('#selectType button'),
			endItem: $("#endItem"),
			selectType: $('#selectType'),
			moreIcon: $('#moreIcon'),
			positionIcon: $(".position-icon"),
			downUp: $('.down_up'),
			stasion: $("#stasion"),
			commit: $("#commit"),
			inputDiv: $("#inputDiv"),
			changeIcon: $(".change-icon")
		};
		// 初始化
		t.init();

		// 事件监听
		t.listen();
	};

	var pt = p.prototype;

	// 页面初始化
	pt.init = function() {
		var t = this;
		t.mapInit();
	};

	// 事件初始化
	pt.listen = function() {
		var t = this;
		t.element.commit.click(function() {
			var s = t.element.stasion.val().trim();
			if(t.page.isStart) {
				t.page.start = s;
			} else {
				t.page.end = s;
			}

			if(isEmpty(s) || t.page.start === t.page.end) {
				mui.alert("请输入站点，并且起点和终点不能相同！")
				return;
			}

			t.page.type = t.element.selectType.find(".active").attr("id");

			t.element.stasion.val("");
			t.element.inputDiv.hide();
			t.element.start.html(t.page.start);
			t.element.end.html(t.page.end);
			if(!isEmpty(t.page.start) && !isEmpty(t.page.end)) {
				t.page.map.clearMap();
				if(t.page.type == 0) {
					routePlan(t.page.start, t.page.end, t.page.city, t.page.map)
				} else if(t.page.type == 1) {
					rid(t.page.start, t.page.end, t.page.city, t.page.map)
				} else if(t.page.type == 2) {
					dir(t.page.start, t.page.end, t.page.city, t.page.map)
				}
			}
		});

		t.element.selectType.on('click', 'button', function() {
			
			t.element.buttons.attr("class" , "");
			$(this).attr('class' , "active");
			t.page.type = this.id;
			if(!isEmpty(t.page.start) && !isEmpty(t.page.end)) {
				t.page.map.clearMap();
				if(t.page.type == '0') {
					routePlan(t.page.start, t.page.end, t.page.city, t.page.map)
				} else if(t.page.type == '1') {
					rid(t.page.start, t.page.end, t.page.city, t.page.map)
				} else if(t.page.type == '2') {
					dir(t.page.start, t.page.end, t.page.city, t.page.map)
				}
			}
		});

		t.element.downUp.on('tap', function() {
			if(t.element.moreIcon.attr("src").indexOf("up") > -1) {
				t.element.moreIcon.attr("src", '../../ui/home/images/search_down.png');
				t.element.panel.css('top', "115px");
				t.element.panel.css('max-height', "100%");
			} else {
				t.element.panel.css('top', "");
				t.element.panel.css('max-height', "0%");
				t.element.moreIcon.attr("src", '../../ui/home/images/search_up.png');
			}
		});

		t.element.changeIcon.bind('tap', function() {
			if(!isEmpty(t.page.start) && !isEmpty(t.page.end)) {
				t.element.start.html(t.page.end);
				t.element.end.html(t.page.start);
				t.page.end = t.page.start;
				t.page.start = t.element.start.html();
				t.page.map.clearMap();
				if(t.page.type == 0) {
					routePlan(t.page.start, t.page.end, t.page.city, t.page.map)
				} else if(t.page.type == 1) {
					rid(t.page.start, t.page.end, t.page.city, t.page.map)
				} else if(t.page.type == 2) {
					dir(t.page.start, t.page.end, t.page.city, t.page.map)
				}
			} else {
				mui.toast("请先输入起点和终点");
			}
		});

		t.element.endItem.bind('tap', function() {
			t.page.isStart = false;
			t.element.stasion.val(t.page.end || "");
			t.element.inputDiv.show();
		})

		t.element.startItem.bind('tap', function() {
			t.page.isStart = true;
			t.element.stasion.val(t.page.start || "");
			t.element.inputDiv.show();
		})

		t.element.positionIcon.on('tap', function() {
			locatioPostion(map)
		});

	};

	// 用户信息初始化
	pt.mapInit = function() {
		var t = this;
		t.page.map = new AMap.Map('container', {
			zoom: 12, //级别
			center: centerPoint, //中心点坐标
			viewMode: '2D' //使用3D视图
		});
		t.page.map.on('complete', function() {
			$(".amap-logo").hide();
			$(".amap-copyright").hide()
		});
		var auto = new AMap.Autocomplete({
			input: "stasion",
			city: city,
			citylimit: true,

		});

		auto.on('select', function(event) {
			console.log(JSON.stringify(event))
			var info = event.poi;
			var s = info.name;
			if(t.page.isStart) {
				t.page.start = s;
			} else {
				t.page.end = s;
			}

			if(isEmpty(s) || t.page.start === t.page.end) {
				mui.alert("请输入站点，并且起点和终点不能相同！")
				return;
			}

			t.page.type = t.element.selectType.find('.active').attr("id");
			if(isEmpty(t.page.type)){
				t.page.type = '0';
			}

			t.element.stasion.val("");
			t.element.inputDiv.hide();
			t.element.start.html(t.page.start);
			t.element.end.html(t.page.end);
			if(!isEmpty(t.page.start) && !isEmpty(t.page.end)) {
				t.page.map.clearMap();
				console.log(t.page.type);
				if(t.page.type == '0') {
					routePlan(t.page.start, t.page.end, t.page.city, t.page.map)
				} else if(t.page.type == '1') {
					rid(t.page.start, t.page.end, t.page.city, t.page.map)
				} else if(t.page.type == '2') {
					dir(t.page.start, t.page.end, t.page.city, t.page.map)
				}
			}
		});

	};

	e.page = new p(config);
	mui.init({
		keyEventBind: {
			backbutton: true //开启back按键监听
		}
	});
	// 页面初始化
	mui.plusReady(function() {

	});

	mui.back = function(event) {
		if($("#inputDiv").css("display") != "none") {
			$("#inputDiv").hide();
		} else {
			history.go(-1);
		}
		return false;
	};

}(window, $, mui);

function routePlan(start, end, city, map) {
	var transferOptions = {
		city: city,
		policy: AMap.TransferPolicy.LEAST_TIME
	};
	//构造公交换乘类,您如果想修改结果展现效果，请参考页面：https://lbs.amap.com/fn/css-style/
	var transfer = new AMap.Transfer(transferOptions);
	//根据起、终点坐标查询公交换乘路线
	transfer.search([{
		keyword: start,
		city: city
	}, {
		keyword: end,
		city: city
	}], function(status, result) {
		if(status == 'complete') {
			(new Lib.AMap.TransferRender()).autoRender({
				data: result,
				map: map,
				panel: "panel"
			});
			$('.down_up').show();
		} else {
			alert(result);
		}
	});
}

function locatioPostion(map) {
	map.plugin('AMap.Geolocation', function() {
		geolocation = new AMap.Geolocation({
			enableHighAccuracy: true, //是否使用高精度定位，默认:true
			timeout: 10000, //超过10秒后停止定位，默认：无穷大
			buttonOffset: new AMap.Pixel(10, 20), //定位按钮与设置的停靠位置的偏移量，默认：Pixel(10, 20)
			zoomToAccuracy: true, //定位成功后调整地图视野范围使定位位置及精度范围视野内可见，默认：false
			buttonPosition: 'RB'
		});
		map.addControl(geolocation);
		geolocation.getCurrentPosition();
	});
}

function rid(start, end, city, map) {
	$('.down_up').hide();
	$('#panel').hide();
	var riding = new AMap.Riding({
		map: map
	});
	riding.search([{
			keyword: start,
			city: city
		},
		{
			keyword: end,
			city: city
		}
	]);
}

function dir(start, end, city, map) {
	var drivingOption = {
		map: map
	};
	var driving = new AMap.Driving(drivingOption);
	driving.search([{
		keyword: start,
		city: city
	}, {
		keyword: end,
		city: city
	}]);
}